/**
 * <p>Title: ServiceZMCM64.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:14:31  余虎  Create
 *</pre>
 */
package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplication;
import com.baosight.sczx.zm.cm.domain.ZMCMStockPlanDetail;

/**采购计划---月采购计划明细
 * <p>Title: ServiceZMCM64.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:14:31  余虎  Create
 *</pre>
 */
public class ServiceZMCMD7 extends ServiceEPBase {


	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM64</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:16:08 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM64</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:16:08 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		inInfo.addBlock("Planresult").set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		String fdStockPlanId = inInfo.getString("fdStockPlanId");
		String fdYear = inInfo.getString("fdYear");
		String fdMonth = inInfo.getString("fdMonth");
		StaticTools.setQueryParameters(inInfo, "fdStockPlanId", fdStockPlanId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdYear", fdYear, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMonth", fdMonth, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCMStockPlanDetail.query", new ZMCMStockPlanDetail(), false, null, EiConstant.queryBlock, "Planresult", "Planresult");
		
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));//构造计量单位下拉框
		return outInfo;
	}
	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM64</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:16:08 余虎
	 * </p>
	 *
	*/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public EiInfo insert(EiInfo inInfo) {
		
       for(int i = 0; i < inInfo.getBlock("result").getRowCount(); i++){
			
			List<Map> list = dao.query("ZMCMStockPlanDetail.queryForCombine", null);
			for( Map map : list){
				String keyId = (String) map.get("fdKeyId");
				String planId = (String) map.get("fdStockPlanId");
				String planCode = (String) map.get("fdMaterialCode");
				String ssum = map.get("fdSum").toString();
				double sum =Double.parseDouble(ssum);
				String fdStockPlanId = inInfo.getBlock("result").getCellStr(i, "fdStockPlanId");
				String fdMaterialCode = inInfo.getBlock("result").getCellStr(i, "fdMaterialCode");
				double fdSum = Double.parseDouble(inInfo.getBlock("result").getCellStr(i, "fdSum"));
				if(fdStockPlanId.equals(planId) && fdMaterialCode.equals(planCode)){
					inInfo.getBlock("result").setCell(i, "fdSum", fdSum+sum);
					inInfo.getBlock("result").setCell(i, "fdKeyId", keyId);
					EiInfo outInfo = super.update(inInfo, "ZMCMStockPlanDetail.updateForSum");//合并数据
					       //outInfo = super.update(inInfo,"ZMCMStockPlan.updateForMaterialNum", "Planresult");//更新种类
					       outInfo = super.update(inInfo,"ZMCMStockPlan.updateForPlanSum");//更新计划总价
					return outInfo;
				}
			}
		String fdStockPlanId = inInfo.getBlock("result").getCellStr(i, "fdStockPlanId");
		inInfo.getBlock("result").setCell(i, "fdStockPlanId", fdStockPlanId);
		inInfo.getBlock("result").setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_STOCK_PLAN_DETAIL"));//主键
		}
		super.insert(inInfo, "ZMCMStockPlanDetail.insert");
		EiInfo outInfo = super.update(inInfo,"ZMCMStockPlan.updateForMaterialNum");//更新种类
		       outInfo = super.update(inInfo,"ZMCMStockPlan.updateForPlanSum");//更新计划总价
		       outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));//构造计量单位下拉框
		return outInfo;
	}
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM64</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:16:08 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdStockPlanId = inInfo.getBlock("result").getCellStr(0, "fdStockPlanId");
		inInfo.getBlock("result").setCell(0, "fdStockPlanId", fdStockPlanId);
		EiInfo outInfo = super.update(inInfo, "ZMCMStockPlanDetail.update");
			   outInfo = super.update(inInfo,"ZMCMStockPlan.updateForMaterialNum");//更新种类
	           outInfo = super.update(inInfo,"ZMCMStockPlan.updateForPlanSum");//更新计划总价
		return outInfo;
	}

	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM64</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:16:08 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//String fdStockPlanId = inInfo.getBlock("result").getCellStr(0, "fdStockPlanId");
		String fdStockPlanId = inInfo.getString("fdStockPlanId").toLowerCase();
		inInfo.getBlock("result").setCell(0, "fdStockPlanId", fdStockPlanId);
		EiInfo _temp = super.delete(inInfo, "ZMCMStockPlanDetail.delete","Planresult");
		_temp = super.update(inInfo,"ZMCMStockPlan.updateForMaterialNum");//更新种类
		_temp = super.update(inInfo,"ZMCMStockPlan.updateForPlanSum", "Planresult");//更新计划总价
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: queryApplicationDepartment</p>
	 * <p>Description: 查询需求计划申请单位</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-3-27下午04:05:48 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo queryApplicationDepartment(EiInfo inInfo) 
	{
		String fdMaterialId = inInfo.getString("fdMaterialId");
		String fdMaterialName = inInfo.getString("fdMaterialName");
		String fdStockPlanId = inInfo.getString("fdStockPlanId");
		
		StaticTools.setQueryParameters(inInfo, "fdMaterialId", fdMaterialId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMaterialName", fdMaterialName, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdStockPlanId", fdStockPlanId, true);//添加查询条件
		
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = super.query(inInfo, "ZMCMStockPlanDetail.queryDepartment", new ZMCMStockPlanDetail());

		return outInfo;

	}
}
